package com.example.android_app.network.services.consmp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.widget.Toast;

import com.example.android_app.GameActivity;
import com.example.android_app.network.Constants;
import com.example.android_app.network.ServiceCall;

public class SimpleTextService implements ServiceCall {
	
	private HttpGet request;
	private DefaultHttpClient httpclient;
	private GameActivity gameActivity;
	
	public SimpleTextService(DefaultHttpClient httpclient, GameActivity gameActivity) {
		// TODO Auto-generated constructor stub
		//this.request = new HttpGet("http://192.168.1.6:8080/services/rest/services/simpletext/HI_THERE");
		//this.request = new HttpGet("http://192.168.1.3:8080/RESTfulExample/rest/hello/simpletext/hi");
		this.request = new HttpGet("http://"+Constants.SERVICE_HOST+":"+Constants.SERVICE_PORT+"/services/rest/services/simpletext/HI_THERE");
		//request.addHeader("Accept", "text/xml");
		this.httpclient = httpclient;
		this.gameActivity = gameActivity;
	}
	
	private static String read(InputStream instream) 
    {
        StringBuilder sb = null;
        try 
        {
             sb = new StringBuilder();
             BufferedReader r = new BufferedReader(new InputStreamReader(instream));
             for (String line = r.readLine(); line != null; line = r.readLine()) 
             {
             	sb.append(line);
             }

			instream.close();

        } catch (IOException e) 
        {
        	
        }
        return sb.toString();

    }
	
	@Override
	public void call() {
		// TODO Auto-generated method stub
		HttpResponse response = null;
		try {
			response = httpclient.execute(request);
			HttpEntity httpEntity = response.getEntity();
			InputStream inputStream = null;
			inputStream = httpEntity.getContent();
			String jaxrsmessage = read(inputStream);
			System.out.println(">>>>>>>>"+jaxrsmessage);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			System.out.println(" >>>>>>>>>>>>>>>>>>> ClientProtocolException Occured <<<<<<<<<<<<<<<<<<<<< ");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			System.out.println(" >>>>>>>>>>>>>>>>>>> IOException Occured <<<<<<<<<<<<<<<<<<<<< ");
		}
		
		
		/*try {
			
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		
		
		//Toast.makeText(gameActivity, jaxrsmessage,Toast.LENGTH_LONG).show();
	}

}
